package com.xcity.game.worldevent;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;
import naga.x.util.StringUtils;

/**
 * 世界事件时间表
 * @author yang.li
 *
 */
@Excel(value = "world_event.xlsx", sheet = 1)
public class WorldEventTimeTemplate extends Template {

	/** 时间点(24时格式,多个时间点逗号隔开) */
	protected String time;

	/** 增益事件可发生次数 */
	protected int goodEventCount;

	/** 减益事件可发生次数 */
	protected int notGoodEventCount;
	
	/** 时间点，加载完后通过time转换 */
	protected long[] timePoints;

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getGoodEventCount() {
		return goodEventCount;
	}

	public void setGoodEventCount(int goodEventCount) {
		this.goodEventCount = goodEventCount;
	}

	public int getNotGoodEventCount() {
		return notGoodEventCount;
	}

	public void setNotGoodEventCount(int notGoodEventCount) {
		this.notGoodEventCount = notGoodEventCount;
	}
	
	public long[] getTimePoints() {
		return timePoints;
	}
	
	public long getTimePoint(int index) {
		return timePoints[index >= timePoints.length ? index % timePoints.length : index];
	}


	@Override
	public void validate() throws TemplateException {
		String[] strs = StringUtils.split(time, ",");
		int len = strs.length;
		timePoints = new long[len];
		DateFormat df = new SimpleDateFormat("HH:mm");
		for (int i = 0; i < len; i++) {
			String t = strs[i];
			try {
				Date date = df.parse(t);
				timePoints[i] = date.getTime() + TimeZone.getDefault().getRawOffset();
			} catch (ParseException e) {
				throw new TemplateException();
			}
		}
		time = null; // gc
	}
	
	public int getWeek() {
		return id;
	}
}
